package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.UserDetailsMapper;
import com.example.communitypro.mapper.UserInformationMapper;
import com.example.communitypro.pojo.*;
import com.example.communitypro.service.HomeMessageService;
import com.example.communitypro.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Queue;

@Service
public class HomeMessageServiceImpl implements HomeMessageService {

    @Autowired(required=false)
    private UserDetailsMapper userDetailsMapper;

    @Override
    public String add(Post post) {

        String userName = "lsl";
        UserDetails userDetails;
        try {
            userDetails = userDetailsMapper.selectByUser(post.getUserNo());
            userName =  userDetails.getUserName();
        }catch (Exception e){
            e.printStackTrace();
        }
        String title = "";
        String context = "";
        Date date = post.getPostTime();

        title = userName+"发布了标题为"+post.getPostTitle()+"的帖子";
        context = post.getPostContent();

        HomeMessage homeMessage = new HomeMessage(title, context, date);
        HomeMessageQueue homeMessageQueue = (HomeMessageQueue) SpringContextUtils.getBean("homeMessageQueue");
        Queue<HomeMessage> homeMessageQueue1 = homeMessageQueue.getHomeMessageQueue();

        if (homeMessageQueue1.size() >= 6){
            homeMessageQueue1.poll();
        }
        homeMessageQueue1.add(homeMessage);
        return userName;

    }

    @Override
    public String add(CommunityInformation communityInformation) {
        String userName = "管理员";
        String title = "";
        String context = "";
        Date date = communityInformation.getCommunityCreationTime();

        title = userName+"创建了标题为"+communityInformation.getCommunityName()+"的社区";
        context = communityInformation.getCommunitySynopsis();

        HomeMessage homeMessage = new HomeMessage(title, context, date);
        HomeMessageQueue homeMessageQueue = (HomeMessageQueue) SpringContextUtils.getBean("homeMessageQueue");
        Queue<HomeMessage> homeMessageQueue1 = homeMessageQueue.getHomeMessageQueue();

        if (homeMessageQueue1.size() >= 6){
            homeMessageQueue1.poll();
        }
        homeMessageQueue1.add(homeMessage);
        return userName;
    }

    @Override
    public String add(UserReport report) {
        String userName = "lsl";
        try {
            userName = userDetailsMapper.selectByUser(report.getUserNo()).getUserName();
        }catch (Exception e){
            e.printStackTrace();
        }
        String title = "";
        String context = "关于用户举报的详细信息，可以通过举报的对象编号查到对应信息";
        Date date = report.getReportTime();

        if(0==report.getReportObjectType()){
            title = userName+"举报了用户";
          //  context = ""+date+"";
        }else if(1==report.getReportObjectType()){
            title = userName+"举报了贴子";
        }else {
            title = userName+"举报了评论";
        }

    //    context = report.

        HomeMessage homeMessage = new HomeMessage(title, context, date);
        HomeMessageQueue homeMessageQueue = (HomeMessageQueue) SpringContextUtils.getBean("homeMessageQueue");
        Queue<HomeMessage> homeMessageQueue1 = homeMessageQueue.getHomeMessageQueue();

        if (homeMessageQueue1.size() >= 6){
            homeMessageQueue1.poll();
        }
        homeMessageQueue1.add(homeMessage);
        return userName;
    }
}
